from aiohttp import web
import jinja2

from pathlib import Path


home_path = str(Path(__file__).parent)
jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(home_path))


def render_html(tmplfile, **kwargs):
    template = jinja_env.get_template(tmplfile)
    text = template.render(**kwargs)
    return web.Response(text=text, content_type="text/html")


async def home_page(request):
    return render_html("home.html.j2")


async def list_page(request):
    items = ["路人甲", "路人乙", "路人丙"]
    title = "这是标题"

    return render_html("list.html.j2", title=title, items=items)

app = web.Application()
app.add_routes([
    web.get('/', home_page),
    web.get('/home', home_page),
    web.get('/list', list_page),
])

if __name__ == "__main__":
    web.run_app(app, port=8080)
